package run.halo.app.theme.router;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import run.halo.app.core.endpoint.theme.ProductQueryEndpoint;
import run.halo.app.core.endpoint.theme.ProductTypeQueryEndpoint;

/**
 * Configuration for product-related public API routes.
 *
 * @author halo-product-management
 * @since 2.0.0
 */
@Configuration
public class ProductRouterConfiguration {

    private final ProductQueryEndpoint productQueryEndpoint;
    private final ProductTypeQueryEndpoint productTypeQueryEndpoint;

    public ProductRouterConfiguration(ProductQueryEndpoint productQueryEndpoint,
                                    ProductTypeQueryEndpoint productTypeQueryEndpoint) {
        this.productQueryEndpoint = productQueryEndpoint;
        this.productTypeQueryEndpoint = productTypeQueryEndpoint;
    }

    /**
     * Product public API router function.
     * 高优先级路由，确保自定义端点优先于默认Extension路由
     * 
     * @return RouterFunction for product APIs
     */
    @Bean
    @Order(-100)  // 设置高优先级，确保在ExtensionCompositeRouterFunction之前
    RouterFunction<ServerResponse> productPublicApiRouter() {
        System.out.println("🚀 注册产品自定义API路由，优先级=-100");
        return RouterFunctions.route()
            .path("/apis/content.halo.run/v1alpha1", () -> RouterFunctions.route()
                .add(productQueryEndpoint.endpoint())
                .add(productTypeQueryEndpoint.endpoint())
                .build())
            .build();
    }
}